// ex03.c -- Redesign the ex02
#include <stdio.h>

void get_info(float* pdistance, float* pfuel, const int mode);
void show_info(const float distance, const float fuel, const int mode);

int main(void) {
  int mode = 0;
  int recent_mode = mode;
  float distance = 0.0f;
  float fuel = 0.0f;

  printf("Enter 0 for metric mode, 1 for US mode: ");
  scanf("%d", &mode);
  while (mode != -1) {
    if (mode != 1 && mode != 0) {
      printf("Invalid mode specified. Mode %d(%s) used.\n", recent_mode,
             1 == recent_mode ? "US" : "metric");
      mode = recent_mode;
    } else {
      recent_mode = mode;
    }
    get_info(&distance, &fuel, mode);
    show_info(distance, fuel, mode);
    printf("Enter 0 for metric mode, 1 for US mode");
    printf(" (-1 to quite): ");
    scanf("%d", &mode);
  }
  printf("Done.\n");
  return 0;
}

void get_info(float* pdistance, float* pfuel, int mode) {
  printf("Enter distance traveled in %s: ", 1 == mode ? "miles" : "kilometers");
  scanf("%f", pdistance);
  printf("Enter fuel consumed in %s: ", 1 == mode ? "gallons" : "liters");
  scanf("%f", pfuel);
}

void show_info(const float distance, const float fuel, const int mode) {
  printf("Fuel consumption is %.*f %s per %s\n", 1 == mode ? 1 : 2,
         1 == mode ? distance / fuel : fuel * 100 / distance,
         1 == mode ? "miles" : "liters", 1 == mode ? "gallons" : "100 km");
}
